import React from 'react'
// ===================================================================== Image
import Z1 from '@img/test-center/status/1.png'	// 超出临床可报告范围
import Z2 from '@img/test-center/status/2.png'	// 超出线性范围
import Z3 from '@img/test-center/status/3.png'	// 资料待审核
import Z4 from '@img/test-center/status/4.png'	// 已外包
import Z5 from '@img/test-center/status/5.png'	// 敏感词
import Z6 from '@img/test-center/status/6.png'	// 阳性

import Img_set from '@img/test-center/set.png' 
import Img_tips from '@img/test-center/tips.png'
import Img_name from '@img/test-center/name.png'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== antd
const Select		= $Lazy.load(()=>import('@antd/form/select'))
const Input 		= $Lazy.load(()=>import('@antd/form/input'))
const Image 		= $Lazy.load(()=>import('@tp/image'))
// 
// ===================================================================== Component
const IconImage = ({src, title, onClick, className, isRight})=> <Image className={className} src={src} alt={title} title={title} width={14} style={ isRight ? { marginLeft:3} : {marginRight:3}} onClick={onClick} />

const $ = {
	//
	onChanged({ value, name }, {uuid}){
		if( name === 'spec_type' ){ // 标本类型
			$http.submit(null,'result-unit-item/updatespectype', { param:{ spec_type:value, uuid} }).then(data=>{
				this.onRefresh()
			})
		}else if( name === 'result_suggestion_id'){ // 建议与解释
			$http.submit(null,'result-unit-item/updateResultSuggestion', { param:{result_suggestion_id:value, uuid} }).then(data=>{
				this.onRefresh()
			})
		}
	},
	// 条码号判断条件
	spec_code(value, rows){
		// 资料待审核
		const waitStatus = (+rows.case_info_status === 0 || +rows.case_info_status === 1) ? <IconImage src={Z3} isRight title='资料待审核' /> : ''
		// 已外包 && 委托检验中心不显示已外包
		
		const outsourcingStatus = (rows.outsourcing_company_code && !this.isEntrust) || (this.isEntrust && +rows.is_option_outsourcing===1) ? <IconImage src={Z4} isRight title='已外包' /> : ''
		// 敏感词
		const sensitiveStatus = +rows.is_sensitive_words === 1 ? <IconImage isRight src={Z5} title='敏感词' /> : ''
		// 阳性  
		let positiveStatus = ''
		if($fn.hasArray(this.pos)){
			const result = this.pos[0].range
			const type = ['dic', 'qualitative', 'other']
			if ( (type.includes(rows.forms.result.type)  && result.includes(rows.result)) ) { 
				positiveStatus = <IconImage src={Z6} isRight title='阳性' />
			}  
		}
		// 超出临床可报告范围
		const overClinicalStatus = +rows.over_clinical === 1 ? <IconImage isRight src={Z1} title='超出临床可报告范围' /> : ''
		// 超出线性范围
		const overLinearStatus = +rows.over_linear === 1 ? <IconImage src={Z2} isRight title='超出线性范围' /> : ''
		return <div className='fxm'>{$fn.getSplitSpecCode(value)}{waitStatus}{outsourcingStatus}{sensitiveStatus}{positiveStatus}{overClinicalStatus}{overLinearStatus}</div> 
	},
	// 点击图标事件,跳转到项目设置页面 
	renderSettings(value, code) {
	    return (
			<div className='fxm'>
			    <IconImage className='cp mr5' src={Img_set} onClick={ e => {
					e.stopPropagation()
					$fn.pushCenter(this,`/test-project?kind_code=${code}`)
				}} />
			    <span>{value}</span>
			</div>
		)
	},
	// 列表 - 下拉/输入框 切换值
	renderSelect({ text, name, data, idStr }, rows){
		return <Select 
				name		= { name }
				value		= { text } 
				data		= { data } 
				isClear		= { false }
				idStr		= { idStr }
				width		= '100%'
				p			= '选择'
				onChanged	= { option => $.onChanged.call(this, option, rows) } 
		/>
	},
	// 输入框
	renderInput({ text, name }, rows){
		return <Input
			className = { name}
			value	= { text } 
			width	= '100%'
			onClick	= { e=> e.stopPropagation() }
			p		= '输入'
			key		= {this.state.key}
			onChange = { value => rows[name] = value}
		/> 
	},
	// 结果
	renderResult ({ text, name }, rows) { 
		let d = text
		const { forms } = rows
		const onChange = value => {
			rows[name] = value
			const { data } = this.state
			this.setState({ data })
		}
		if (forms && forms[name] && forms[name].type) { 
			switch (forms[name].type) {
				case 'text': 
					d = <Input key={this.state.key} width='100%' p='输入结果' className={name} value={text} onClick={e=>e.stopPropagation()} onChange={onChange} /> 
					break
				case 'number':  
					d = <div className='fxm'> 
						<IconImage className='cp mr5' src={Img_tips} title='参考范围' onClick={ e => { 
							e.stopPropagation()
							this.setState({ reference_range: rows['reference_range'] })
							this.drawer.open()
						}} /> 
						<Input key={this.state.key} className={name} p='输入结果' value={text} onClick={e=>e.stopPropagation()} onChange={onChange} /> 
					</div>
					break
				case 'dict':
					d = <Select p='选择结果' data={forms[name].select} value={text}  width='100%'  onChange={onChange} />
				break
				case 'qualitative': 
					d = <Select p='选择结果' data={forms[name].select} value={text}  width='100%'  onChange={onChange} />
					break
				case 'compute': 
					break 
				case 'other': 
					break
				default: 
					break
	        }
	    } 
	    return d
	},
}

const _ = {
	// 搜索列
	onSearch(e, name, text){
		e.stopPropagation()
		this.props.searchRef().onSubmit({ [name]:text })
		this.props.searchRef().setValue({ [name]:text })
	},
	col(title, name){
		return {
			text: (title,name) => ({ title: title, dataIndex: name, 	width:150 }),
			time: (title,name) => ({ title: title, dataIndex: name,  ...$fn.table.time }),
		}
	},
	cols(){
		return {
			spec_code			: { title: '标本条码', 			dataIndex: 'spec_code', 			width:170, 					sorter:true, 	render: (text, rows) => <span onDoubleClick={e=>_.onSearch.call(this,e,'spec_code', text)}>{$.spec_code.call(this, text, rows)}</span> },
			parent_kind_name 	: { title: '自然项目', 			dataIndex: 'parent_kind_name', 		width:250,  				sorter:true, 	render:(text, { parent_kind_id, parent_kind_code })=> <span onDoubleClick={e=>_.onSearch.call(this,e,'parent_kind_id', parent_kind_id)}>{$.renderSettings.call(this,text, parent_kind_code)}</span> },
			
			experiment_num 		: { title: '实验号', 			dataIndex: 'experiment_num', 		width:100, 					sorter:true, render: text => <span onDoubleClick={e=>_.onSearch.call(this,e,'experiment_num', text)}>{text}</span> },
			experiment_num2 	: { title: '病理号', 			dataIndex: 'experiment_num', 		width:100,			 		sorter:true, render: text => <span onDoubleClick={e=>_.onSearch.call(this,e,'experiment_num', text)}>{text}</span> },
			
			kind_name			: { title: '项目名称', 			dataIndex: 'kind_name', 			width:300, 					sorter:true, 	render:(text, {kind_id, kind_code})=> <span onDoubleClick={e=>_.onSearch.call(this,e,'kind_id', kind_id)}>{$.renderSettings.call(this,text, kind_code)}</span> },
			patient_name		: { title: '姓名', 				dataIndex: 'patient_name', 			...$fn.table.name, width:110, render: (text, rows)=>{
				return +rows.check_same_name === 1 ? <div className='fxm'  onDoubleClick={e=>_.onSearch.call(this,e,'patient_name', text)}><IconImage className='rel' style={{left:-5}} src={Img_name}/><span>{text}</span></div> : <span onDoubleClick={e=>_.onSearch.call(this,e,'patient_name', text)}>{text}</span>
			} },
			spec_type			: { title: '标本类型', 			dataIndex: 'spec_type', 			width:200,									render: (text, rows) => $.renderSelect.call(this, { text, name:'spec_type', data:this.state.sampleTypeData }, rows) },
			result				: { title: '结果', 				dataIndex: 'result', 				width:150, 					sorter:true, 	render: (text, rows) => $.renderResult.call(this, { text, name:'result' }, rows) },
			result1				: { title: '报告结果', 			dataIndex: 'result1', 				width:100, 									render: (text, rows)=>{
				return rows?.kd_check_result_cats?.result1 === '1' ? $.renderInput.call(this,{text,name:'result1'}, rows) : text
			}},
			unit_val_name		: { title: '单位', 				dataIndex: 'unit_val_name', 		width:80 },
			tips				: { title: '提示', 				dataIndex: 'tips', 					width:200, 					sorter:true, render: text => {
				if( text === '↑↑' || text === '↑' ){
					return <span style={{color:'red'}}>{text}</span>
				}else if( text === '↓↓' || text === '↓'){
					return <span style={{color:'blue'}}>{text}</span>
				}else{
					return text
				}
			} },
			result_suggestion_id: { title: '建议与解释', 		dataIndex: 'result_suggestion_id', 	width:200,									render: (text, rows) => $.renderSelect.call(this, { text, name:'result_suggestion_id', data: rows.result_suggestion, idStr:'uuid' }, rows)},
			dilution_multiple	: { title: '稀释倍数', 			dataIndex: 'dilution_multiple', 	width:80,  									render: (text, rows) =>  $.renderInput.call(this, { text, name:'dilution_multiple' }, rows) },
			is_delay			: { title: '迟发状态', 			dataIndex: 'is_delay', 				width:80, 									render: text => (+text === 0 ? '否' : '是'), align:'center' },
			delay_at			: { title: '迟发预计报告时间', 	dataIndex: 'delay_at', 				width:200 },
			delay_reason		: { title: '迟发原因', 			dataIndex: 'delay_reason', 			width:200 },
			status_abnormal_name: { title: '异常状态', 			dataIndex: 'status_abnormal_name', 	width:80, render: (text, { status }) => {
				return +status === 45007 || +status === 45008 ? text : ''
			} },
			ts_sn				: { title: '项目检测编号', 		dataIndex: 'ts_sn', 				width:150 },
			kind_code			: { title: '项目编码', 			dataIndex: 'kind_code', 			width:70, render: text => <span onDoubleClick={e=>_.onSearch.call(this,e,'kind_code', text)}>{text}</span> },
			hosp_name			: { title: '医院名称', 			dataIndex: 'hosp_name', 			width:200 },
			receive_time		: { title: '接收时间', 			dataIndex: 'receive_time', 			...$fn.table.time, sorter:true },
			third_hosp_name		: { title: '第三方医院', 		dataIndex: 'third_hosp_name', 		width:200 },
			id_code				: { title: '图标', 				dataIndex: 'id_code', 				width:200 },
			//
			sex_name			: { title: '性别', 				dataIndex: 'sex_name', 				...$fn.table.sex },
			age					: { title: '年龄', 				dataIndex: 'age', 					...$fn.table.age },
			//
			ts_user				: { title: '主检人', 			dataIndex: 'ts_user', 				...$fn.table.name },
			ts_audit_user		: { title: '审核人', 			dataIndex: 'ts_audit_user', 		...$fn.table.name },
			ts_approve_user		: { title: '批准人', 			dataIndex: 'ts_approve_user', 		...$fn.table.name },
			//
			check_time			: { title: '检验日期', 			dataIndex: 'check_time', 			width:150 },
			outpatient			: { title: '住院号', 			dataIndex: 'outpatient', 			width:150 },
			case_type			: { title: '类型', 				dataIndex: 'case_type', 			width:150 },
			bed_no				: { title: '床号', 				dataIndex: 'bed_no', 				width:150 },
			remark				: { title: '备注', 				dataIndex: 'remark', 				width:150 },
			department_name		: { title: '科室', 			dataIndex: 'department_name', 			width:150 },
		}
	}
}

export default _